package demo.bocweb.com.book.presenter;

import android.support.annotation.NonNull;

import demo.bocweb.com.book.bean.book.BookListBean;
import demo.bocweb.com.book.contract.BookContract;
import demo.bocweb.com.book.model.BookModel;
import demo.bocweb.com.sdk.util.ToastUtils;
import io.reactivex.functions.Consumer;

/**
 * Created by zhelun on 2018/1/22.
 */

public class BookPresenter extends BookContract.BookPresenter {

    @NonNull
    public static BookPresenter newInstance() {
        return new BookPresenter();
    }

    @Override
    public void getBookList(String tag, int start, int limit) {
        if (mIModel == null || mIView == null) {
            return;
        }
        mRxManager.register(mIModel.getBookListWithTag(tag,start, limit)
                .subscribe(new Consumer<BookListBean>() {
                    @Override
                    public void accept(BookListBean bookListBean) throws Exception {
                        if (mIView == null)
                            return;
                        mIView.updateListData(bookListBean.getBooks());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (mIView == null)
                            return;
                        ToastUtils.showToast("网络异常");
                    }
                })
        );    }

    @Override
    public BookContract.BookModel getModel() {
        return BookModel.newInstance();
    }

    @Override
    public void onStart() {

    }
}